package com.ang.deadLock;

public class Test01 {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";

        new Thread(new RunnableA(lockA,lockB),"A").start();
        new Thread(new RunnableA(lockB,lockA),"B").start();
    }
}

class RunnableA implements Runnable {
    String lockA;
    String lockB;

    public RunnableA(String a, String b) {
        lockA = a;
        lockB = b;
    }

    @Override
    public void run() {
        ShareA a = new ShareA();
        try {
            a.test(lockA,lockB);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

class ShareA {
    public void test(String lockA, String lockB) throws InterruptedException {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "\t," + lockA);

            Thread.sleep(500);
            //第二个synchronized，要放在第一个synchronized内部，要造成lockA不释放的情况下，同时等待lockB
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "\t," + lockB);
            }
        }
    }
}
